package com.sihai.ehr.web.action.exam;

import com.sihai.core.LimitedList;
import com.sihai.core.beans.BeanInject;
import com.sihai.ehr.model.biz.EmployeeinfoWork;
import com.sihai.ehr.model.biz.ExamBaseConfig;
import com.sihai.ehr.model.biz.ExamQuanItemSet;
import com.sihai.ehr.model.biz.ExamQuanSet;
import com.sihai.ehr.model.sys.ExaminePeriodEnum;
import com.sihai.ehr.model.sys.Organization;
import com.sihai.ehr.service.*;
import com.sihai.ehr.web.action.EHRAction;
import com.sihai.ehr.web.pojo.ExamItemSetPojo;
import com.sihai.ehr.web.pojo.StaffSearchPojo;
import org.apache.commons.lang.xwork.StringUtils;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;

import java.util.Iterator;
import java.util.List;

/**
 * 考核项目设定
 *
 * @author freshman
 */
@Results({
        @Result(name = "main", location = "exam_itemset_main.jsp"),
        @Result(name = "list", location = "exam_itemset_list.jsp"),
        @Result(name = "setup_list", location = "exam_itemset_setup_list.jsp"),
        @Result(name = "add", location = "exam_itemset_setup_add.jsp"),
        @Result(name="setup_main",location="exam_itemset_setup_main.jsp")
})
public class ExamItemSetAction extends EHRAction {

    @BeanInject
    private OrganizationService organizationService;
    @BeanInject
    private DutyService dutyService;
    @BeanInject
    private ExamItemSetService examItemSetService;
    @BeanInject
    private EmployeeService employeeService;
    @BeanInject
    private ExamBaseConfigService examBaseConfigService;

    private List<Organization> organizations;

    private LimitedList<ExamItemSetPojo> examItemSetPojos;      //职务对应的考核项目列表

    private LimitedList<ExamQuanItemSet> examQuanItemSets;  //考核项目

    private LimitedList<EmployeeinfoWork> employeeinfoWorks;
    
    private ExamQuanItemSet examitem;

    private ExamQuanSet examQuanSet;

    private Long dutyId;//职务ID

    private Boolean edit;//是否编辑
    private String pageTitle;//页面标题
    private String itemIds;//要删除的ID
    private Float maxRate;//已经保存的比率

    private ExaminePeriodEnum periodEnum;

    /**
     * 考核项目设定入口
     *
     * @return
     * @throws Exception
     */
    @Action("/exam/itemset_main")
    @Override
    public String execute() throws Exception {
        listAction = "/exam/itemset_list.xhtml";
        listTitle = "考核项目设定";

        organizations = organizationService.findChildren(null, true, null, null);
        if (organizations != null && organizations.size() > 0) {
        	if(getCurrentUserId() > 0){
            	Long topOrgId = getCurrentUser().getWork().getOrganization().getTopOrganization().getId();
            	Iterator<Organization> it = organizations.iterator();
            	while(it.hasNext()){
            		Organization o = it.next();
            		if(o.getId() != topOrgId){
            			it.remove();
            		}
            	}
            }
            organizationId = organizations.get(0).getId();
        }
        showChildOrg = true;
        return "main";
    }

    /**
     * 考核项目设定列表
     *
     * @return
     * @throws Exception
     */
    @Action("/exam/itemset_list")
    public String list() throws Exception {

        examItemSetPojos = examItemSetService.findByTopOrganization(organizationId, createLimit(), createSorter());
        // 分页
        this.paging(examItemSetPojos);
        this.setPageUrl("exam/itemset_list.xhtml");

        return "list";
    }

    /**
     * 考核项目设定-设置
     *
     * @return
     * @throws Exception
     */
    @Action("/exam/itemset_setup_list")
    public String setting() throws Exception {
        examQuanItemSets = examItemSetService.findByExamQuanSet(examQuanSet.getId(), createLimit(), createSorter());
        maxRate= examItemSetService.getItemSumByQuanSetId(examQuanSet.getId());
        return "setup_list";
    }

    @Action("/exam/itemset_setup_main")
    public String setupMain() throws Exception{
         listAction = "/exam/itemset_setup_list.xhtml?examQuanSet.id="+examQuanSet.getId();
         listTitle = "考核项目设定-设置";
         if (examQuanSet != null) {
            if(examQuanSet.getId()!=null) {
                examQuanSet=examItemSetService.getExamQuanSet(examQuanSet.getId());
            }
            examQuanSet.setDuty(dutyService.get(examQuanSet.getDuty().getId()));
            maxRate= examItemSetService.getItemSumByQuanSetId(examQuanSet.getId());
        }
         return "setup_main";
    }
    /**
     * 考核项目添加，修改
     *
     * @return
     * @throws Exception
     */
    @Action("/exam/item_create")
    public String create() throws Exception {
        if (examitem.getId() != null) {//edit
            ExamQuanItemSet item=examItemSetService.findExamQuanItemSetById(examitem.getId());
            item.setTitle(examitem.getTitle());
            item.setPercentage(examitem.getPercentage());
            item.setRemark(examitem.getRemark());
            item.setDataEntierType(examitem.getDataEntierType());
            item.setDataEntrier(examitem.getDataEntrier());
            item.setDataEntryDuty(examitem.getDataEntryDuty());
            item.setDataAuditerType(examitem.getDataAuditerType());
            item.setDataAuditer(examitem.getDataAuditer());
            item.setDataAuditDuty(examitem.getDataAuditDuty());
            examItemSetService.saveExamItem(item);
        } else {// add
            if (examQuanSet != null) {
                if (examQuanSet.getId() !=null && examQuanSet.getId()>0) {
                    examQuanSet = examItemSetService.getExamQuanSet(examQuanSet.getId());
                    examQuanSet.setItemCount(examQuanSet.getItemCount() + 1);
                } else {
                    examQuanSet.setItemCount(1);
                }
                examItemSetService.saveExamQuanSet(examQuanSet);
                examitem.setExamQuanSet(examQuanSet);
                examitem.setDuty(examQuanSet.getDuty());
                examItemSetService.saveExamItem(examitem);
            }
        }
        return setupMain();
    }

    /**
     * 跳转到添加界面
     *
     * @return
     * @throws Exception
     */
    @Action("/exam/itemset_setup_add")
    public String add() throws Exception {
        edit = false;
        if (examitem != null) {
            pageTitle = "修改";
            examitem = examItemSetService.findExamQuanItemSetById(examitem.getId());
        } else {
            pageTitle = "新增";
            //StaffSearchPojo search = new StaffSearchPojo();
            //search.setDepartment(organizationId);
            //employeeinfoWorks = employeeService.searchEmployeeinfoWorks(search, organizationId, "1", createLimit(), createSorter());
        }
        organizations = organizationService.findChildren(organizationId, false, null, null);
        try{
            ExamBaseConfig baseConfig = examBaseConfigService.getExamBaseConfigByDutyId(examQuanSet.getDuty().getId());
            periodEnum = baseConfig.getPeriodType();
        }catch (Exception e){
            e.printStackTrace();
        }

        return "add";
    }

    /**
     * 删除定量考核项目
     *
     * @return
     * @throws Exception
     */
    @Action("/exam/quanitem_delete")
    public String remove() throws Exception {

        if (StringUtils.isBlank(itemIds)) {
            return setupMain();
        }
        examItemSetService.removeQuanItem(itemIds, examQuanSet.getId());
        return setupMain();

    }

    public List<Organization> getOrganizations() {
        return organizations;
    }

    public void setOrganizations(List<Organization> organizations) {
        this.organizations = organizations;
    }

    public ExamQuanItemSet getExamitem() {
        return examitem;
    }

    public void setExamitem(ExamQuanItemSet examitem) {
        this.examitem = examitem;
    }

    public LimitedList<ExamItemSetPojo> getExamItemSetPojos() {
        return examItemSetPojos;
    }

    public void setExamItemSetPojos(LimitedList<ExamItemSetPojo> examItemSetPojos) {
        this.examItemSetPojos = examItemSetPojos;
    }

    public ExamQuanSet getExamQuanSet() {
        return examQuanSet;
    }

    public void setExamQuanSet(ExamQuanSet examQuanSet) {
        this.examQuanSet = examQuanSet;
    }

    public LimitedList<ExamQuanItemSet> getExamQuanItemSets() {
        return examQuanItemSets;
    }

    public void setExamQuanItemSets(LimitedList<ExamQuanItemSet> examQuanItemSets) {
        this.examQuanItemSets = examQuanItemSets;
    }

    public Long getDutyId() {
        return dutyId;
    }

    public void setDutyId(Long dutyId) {
        this.dutyId = dutyId;
    }

    public Boolean getEdit() {
        return edit;
    }

    public void setEdit(Boolean edit) {
        this.edit = edit;
    }

    public String getPageTitle() {
        return pageTitle;
    }

    public void setPageTitle(String pageTitle) {
        this.pageTitle = pageTitle;
    }

    public String getItemIds() {
        return itemIds;
    }

    public void setItemIds(String itemIds) {
        this.itemIds = itemIds;
    }

    public Float getMaxRate() {
        return maxRate;
    }

    public void setMaxRate(Float maxRate) {
        this.maxRate = maxRate;
    }

    public LimitedList<EmployeeinfoWork> getEmployeeinfoWorks() {
        return employeeinfoWorks;
    }

    public void setEmployeeinfoWorks(LimitedList<EmployeeinfoWork> employeeinfoWorks) {
        this.employeeinfoWorks = employeeinfoWorks;
    }

    public ExaminePeriodEnum getPeriodEnum() {
        return periodEnum;
    }
}
